package com.czl.module_service.fragment.inventory

import com.alibaba.android.arouter.facade.annotation.Route
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.CategoryBean
import com.czl.base.data.bean.tengyun.OwnerCompanyBean
import com.czl.base.util.ChoiceImgHelper
import com.czl.module_base.widget.timeselector.TimeSelector
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.databinding.FragmentAssetRegisterBinding
import com.czl.module_service.view.CategoryPopup
import com.czl.module_service.view.ListSheetDialogFragment
import com.czl.module_service.viewmodel.inventory.AssetRegisterViewModel
import com.lxj.xpopup.XPopup
import java.util.*

@Route(path = AppConstants.Router.Service.F_SERVICE_ASSET_REGISTER)
class AssetRegisterFragment :
    BaseFragment<FragmentAssetRegisterBinding, AssetRegisterViewModel>() {
    override fun initContentView(): Int {
        return R.layout.fragment_asset_register
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun initData() {
        super.initData()
        viewModel.tvTitle.set("盘盈登记")
//        tid=372&execId=d9790a978fc04654bfbc8f9867e2033c&taskName=固定资产06110002
        viewModel.taskId = // 5
            arguments?.getSerializable("taskId") as Long
//        viewModel.taskName =// "测试固定资产盘点0001"
//            arguments?.getSerializable("taskName") as String
        viewModel.execId = //"b076466b234f44c1aad6dd8dd625d568"
            arguments?.getSerializable("execId") as String
        viewModel.getBaseInfo()
        viewModel.getIdByNeighNo()
    }

    override fun initViewObservable() {
        viewModel.uc.loadBackEvent.observe(this, {
            back()
        })
        viewModel.uc.loadChangeEvent.observe(this, { data ->
            if (data == 0) {

            } else if (data == 1) {// 隐藏资产
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    viewModel.isHideArr
                ) {
                    viewModel.isHide = it
                    viewModel.isHideObs.set(viewModel.isHideArr[it])
                }
            } else if (data == 2) {// 项目
                val keyArr = viewModel.projectMap.keys.toList() as MutableList
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    keyArr
                ) {
                    var keyVal = keyArr[it]
                    viewModel.projectObs.set(keyVal)
                    viewModel.projectId = viewModel.projectMap.get(keyVal)?.areaId!!
                }
            } else if (data == 3) {// 取得方式
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    viewModel.gainMethodArr
                ) {
                    viewModel.gainMethodObs.set(viewModel.gainMethodArr[it])
                    viewModel.gainMethod = it + 1
                }
            } else if (data == 4) {// 管理公司
                if (viewModel.companyMap.isEmpty()) {
                    showNormalToast("未查询到公司信息！")
                    return@observe
                }
                val keyArr = viewModel.companyMap.keys.toList() as MutableList
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    keyArr
                ) {
                    var keyVal = keyArr[it]
                    viewModel.manageCompanyObs.set(keyVal)
                    viewModel.manageCompanyId = viewModel.companyMap.get(keyVal)?.companyId!!
                    viewModel.getManageOrgList()
                }
            } else if (data == 5) {// 产权公司
                if (viewModel.companyMap.isEmpty()) {
                    showNormalToast("未查询到公司信息！")
                    return@observe
                }
                val keyArr = viewModel.companyMap.keys.toList() as MutableList
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    keyArr
                ) {
                    var keyVal = keyArr[it]
                    viewModel.parentCompanyObs.set(keyVal)
                    var com = viewModel.companyMap.get(keyVal)
                    val ownerCompanyBean = OwnerCompanyBean()
                    ownerCompanyBean.companyId = com?.companyId.toString()
                    ownerCompanyBean.companyName = com?.companyName
                    ownerCompanyBean.companyShortName = com?.companyShortName
                    ownerCompanyBean.companyRatio = "100"
                    viewModel.parentCompany = ownerCompanyBean
                }
            } else if (data == 6) {// 管理部门
                if (viewModel.orgMap.isEmpty()) {
                    if (viewModel.manageCompanyId > 0) {
                        showNormalToast("管理公司未查询到部门信息！")
                        return@observe
                    }
                    showNormalToast("请选择管理公司")
                    return@observe
                }
                val keyArr = viewModel.orgMap.keys.toList() as MutableList
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    keyArr
                ) {
                    var keyVal = keyArr[it]
                    viewModel.manageOrgObs.set(keyVal)
                    viewModel.manageOrgId = viewModel.orgMap.get(keyVal)?.organizationId!!
                    viewModel.getManageUseList()
                }
            } else if (data == 7) {// 责任人
                if (viewModel.userMap.isEmpty()) {
                    if (viewModel.manageOrgId > 0) {
                        showNormalToast("管理部门未查询到人员信息！")
                        return@observe
                    }
                    showNormalToast("请选择管理部门！")
                    return@observe
                }
                val keyArr = viewModel.userMap.keys.toList() as MutableList
                ListSheetDialogFragment.showBottomSheet(
                    parentFragmentManager,
                    keyArr
                ) {
                    var keyVal = keyArr[it]
                    viewModel.manageUserObs.set(keyVal)
                    viewModel.manageUserId = viewModel.userMap.get(keyVal)?.userId!!
                }
            } else if (data == 8) {// 取得日期
                val timeSelector = TimeSelector(context, { time ->
                    viewModel.gainDateObs.set(time)
                }, "1990-01-01", "2099-12-31", TimeSelector.FORMAT_STR2, Date())
                timeSelector.setScrollUnit(
                    TimeSelector.SCROLLTYPE.YEAR,
                    TimeSelector.SCROLLTYPE.MONTH,
                    TimeSelector.SCROLLTYPE.DAY
                )
                timeSelector.show()
            } else if (data == 9) {// 取得日期
                showCategoryPopup()
            } else if (data == 99) {
                ChoiceImgHelper.ChoiceImg(this,
                    { result -> result[0]?.let { t -> viewModel.uploadHeadImg(t.compressPath) } },
                    {})
            }
        })
    }

    private fun showCategoryPopup() {
        val pop = CategoryPopup(requireContext()) { it ->
            val name: String? = it?.name
            var ids: List<Int>? = null
            it?.childIds?.let {
                ids = ArrayList<Int>(it)
            }
            viewModel.classificationObs.set(name)
            viewModel.classificationId = it?.id!!
            viewModel.getFacilityNoByRule(classificationCode = it.classificationCode)
        }
        pop.setList(viewModel.classificationList)
        XPopup.Builder(context)
            .asCustom(pop)
            .show()
    }
}